import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/merchant",
    name: "merchant",
    component: () => import("../views/Merchant/Merchant.vue"),
    children: [
      {
        path: "merchantAdd",
        name: "merchantAdd",
        component: () => import("../components/Merchant/MerchantAdd.vue"),
      },
      {
        path: "merchantUpdate",
        name: "merchantUpdate",
        component: () => import("../components/Merchant/merchantUpdate.vue"),
      },
      {
        path: "MerchantRead",
        name: "MerchantRead",
        component: () => import("../components/Merchant/MerchantRead.vue"),
      },
      {
        path: "goodslist",
        name: "Goodslist",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/goodsview/goodslist.vue"
          ),
      },
      {
        path: "goodsuplist",
        name: "Goodsuplist",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../components/goods/goodsup.vue"
          ),
      },
      {
        path: "goodsadd",
        name: "Goodsadd",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/goodsview/goodsadd.vue"
          ),
      },
      {
        path: "goodsupdate/:_id",
        name: "Goodupdate",
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../components/goods/update.vue"
          ),
      },
      {
        path: "orderlist",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../components/myOrder/myOrder.vue"
          ),
      },
      {
        path: "updataOrder/:_id",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../components/myOrder/chengeOrder.vue"
          ),
      },
      {
        path: "typeslist",
        name: "Typelist",

        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/typesview/typeslist.vue"
          ),
      },
    ],
  },
  {
    path: "/merchantRegister",
    name: "merchantRegister",
    component: () => import("../views/MerchantRegister/MerchantRegister.vue"),
  },

  //下面三个是管理员的路径,/AdminSystem1是超管,/AdminSystem2是普通管理员
  {
    path: "/AdminLogin",
    component: () => import("../views/admin/AdminLogin.vue"),
  },
  {
    path: "/AdminSystem1",
    component: () => import("../views/admin/AdminSystem1.vue"),
    children: [
      {
        path: "AdminList",
        component: () => import("../components/admin/AdminList.vue"),
      },
      {
        path: 'AdminUpdate/:_id',
        component: () => import('../components/admin/AdminUpdate.vue')
      },
      {
        path: "MerchantList",
        component: () => import("../components/admin/MerchantList.vue"),
      },
      {
        path: 'ShopList',
        component: () => import('../components/admin/ShopList.vue')
      },
      {
        path: 'ShopCheck',
        component: () => import('../components/admin/ShopCheck.vue')
      }
    ]
  },
  {
    path: '/AdminSystem2',
    component: () => import('../views/admin/AdminSystem2.vue'),
    children: [
      {
        path: 'MerchantList',
        component: () => import('../components/admin/MerchantList.vue')
      },
      {
        path: 'ShopList',
        component: () => import('../components/admin/ShopList.vue')
      },
      {
        path: 'ShopCheck',
        component: () => import('../components/admin/ShopCheck.vue')
      }
    ]
  },

  //商铺的 路径 不要覆盖掉了，我后面加到你们一级路径里面
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path === "/merchant") {
    if (sessionStorage.getItem("userName") != null) {
      next();
    } else {
      alert("未登录，请先登录");
      next("/merchantRegister");
    }
  } else {
    next();
  }
})
// 进入路由前方法勾子
router.beforeEach((to, from, next) => {
  /*
    to 目标路由
    from 源路由
    next 跳转到下一个路由
  */
  if (to.path === '/merchant/MerchantRead') {
    // 登录成功先去请求数据，看看开门店没有。然后保存的store里
    if (sessionStorage.getItem('isShop')) {
      next();
    }else{
      next('/merchant/merchantAdd');
    }
  }else{
    next();
  }
});
export default router;
